unit uRefugoRet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai_new, Mask, JvExMask, JvToolEdit, JvBaseEdits, StdCtrls,
  Menus, DB, Buttons, ExtCtrls, MMJPanel, FMTBcd, SqlExpr, DBClient,
  Provider, Grids, DBGrids, JvExDBGrids, JvDBGrid, DBXpress;

type
  TfRefugoRet = class(TfPai_new)
    sqlBusca: TSQLQuery;
    gridOf: TJvDBGrid;
    RefOf: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Motivo: TLabel;
    Label5: TLabel;
    RefQtde: TJvCalcEdit;
    edProduto: TEdit;
    edMotivo: TEdit;
    edOf: TEdit;
    Label3: TLabel;
    sqlRef: TSQLDataSet;
    dspRef: TDataSetProvider;
    cdsRef: TClientDataSet;
    dsRefugo: TDataSource;
    edProdOf: TEdit;
    RefProd: TComboBox;
    sqlP: TSQLDataSet;
    dspP: TDataSetProvider;
    cdsP: TClientDataSet;
    spRetorna: TSQLStoredProc;
    cbLocal: TComboBox;
    qBusca: TSQLQuery;
    Label4: TLabel;
    Data1: TJvDateEdit;
    BitBtn1: TBitBtn;
    cdsRefETI_CODPRO: TStringField;
    cdsRefETI_QTDENV: TFMTBCDField;
    cdsRefPRO_DESCRI: TStringField;
    SQLDataSet1: TSQLDataSet;
    procedure btnGravarClick(Sender: TObject);
    procedure RefOfExit(Sender: TObject);
    procedure RefProdExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    function busca(sql: String): String;
    procedure buscaRefugo;
    { Private declarations }
  public
    codpro: string;
    { Public declarations }
  end;

var
  fRefugoRet: TfRefugoRet;

implementation

uses udm, uEtiqReimprime;

{$R *.dfm}

procedure TfRefugoRet.btnGravarClick(Sender: TObject);
var sMensagem, str : String;
 Save_Cursor:TCursor;
 TD: TTransactionDesc;
 qtde : Double;
begin
  TD.TransactionID := 1;
  TD.GlobalID := 0;
  TD.IsolationLevel := xilREADCOMMITTED;

  //SP_Bloqueia.ParamByName('W_NUMCON').AsFMTBCD := DoubleToBCD(FieldByName('PCC_NUMCON').AsFloat);
  DecimalSeparator := '.';
  qtde := StrToFloat(refQtde.Text);

  // Refugo de Materiais
  Save_Cursor := Screen.Cursor;
  try
    Screen.Cursor := crHourGlass;
    {if (spRetorna.Active) then
      spRetorna.Close;
    spRetorna.ParamByName('COFDET').AsString  := RefOf.Text;
    spRetorna.ParamByName('CODPRO').AsString  := RefProd.Text;
    spRetorna.ParamByName('QTDE').AsFMTBCD    := DoubleToBcd(qtde);
    spRetorna.ParamByName('LOCAL').AsString   := cbLocal.Text;
    spRetorna.ParamByName('USUARIO').AsString := dm.user;
    spRetorna.ParamByName('DATMOV').AsDateTime  := Data1.Date;}

    str := 'INSERT INTO TMP_ATS_REFUGO(COD_OFDET, COD_PRO, QTDE, COLETOR,'+
      ' USUARIO, DTAAPONT, TIPO, LOCAL) VALUES( ' +
      RefOf.Text + ', ' + QuotedStr(RefProd.Text) + ', ' + FloatToStr(qtde) + ', ' +
      QuotedStr('COLETOR') + ', ' + QuotedStr(dm.user) +
      ', to_date(' + QuotedStr(FormatDateTime('dd/mm/yyyy', Data1.Date)) +
      ', '+ QuotedStr('dd/mm/yyyy') +  '), ' + QuotedStr('REAP') + ', ' +
      QuotedStr(cbLocal.Text) + ')';

    DecimalSeparator := ',';

  Try
    dm.SQLConnection1.StartTransaction(TD);
    //spRetorna.ExecProc;
    dm.SQLConnection1.ExecuteDirect(str);
    dm.SQLConnection1.Commit(TD);
    MessageDlg('Executado com sucesso.', mtInformation, [mbOK],0);
    buscaRefugo;
    dm.SQLConnection1.StartTransaction(TD);
    dm.SQLConnection1.ExecuteDirect('DELETE FROM TMP_ATS_REFUGO WHERE USUARIO = ' +
      QuotedStr(dm.user));
    dm.SQLConnection1.Commit(TD);
    refOf.SetFocus;
  except
    on E: EDatabaseError do
    begin
      ShowMessage('Classe: ' + e.ClassName + chr(13) + 'Mensagem: ' + e.Message);
      sMensagem := dm.TraduzErro(E.Message, sMensagem);
      dm.SQLConnection1.Rollback(TD);
      if (sMensagem <> '') then
      begin
        MessageDlg(sMensagem, mtWarning, [mbOK],0);
        exit;
      end
      else begin
        MessageDlg('ERRO.', mtWarning, [mbOK],0);
        exit;
      end;
    end;
  end;
  finally
    Screen.Cursor := Save_Cursor;
  end;
end;

function TfRefugoRet.busca(sql: String): String;
begin
  if (sqlBusca.Active) then
    sqlBusca.Close;
  sqlBusca.SQL.Clear;
  sqlBusca.SQL.Add(sql);
  sqlBusca.Open;
  result := sqlBusca.Fields[0].AsString;
end;

procedure TfRefugoRet.RefOfExit(Sender: TObject);
var str1: String;
begin
  if (RefOf.Text <> '') then
  begin
    edOf.Text := busca('SELECT DOF_CODIOF FROM ATS_OF, ATS_DETOF ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND DOF_CODIGO = ' +
      RefOf.Text);

    codpro := busca('SELECT COF_CODPRO FROM ATS_OF, ATS_DETOF ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND DOF_CODIGO = ' +
      RefOf.Text);
    edProdOf.Text := busca('SELECT COF_CODPRO ||' + QuotedStr('-') + ' || PRO_DESCRI ' +
      ' FROM ATS_OF, ATS_DETOF, F_PRODS ' +
      ' WHERE DOF_CODIOF = COF_CODIOF AND COF_CODPRO = PRO_CODPRO ' +
      ' AND DOF_CODIGO = ' +
      RefOf.Text);
  end;
  if (cdsP.Active) then
    cdsP.Close;
  str1 := 'SELECT OFC_CODPRO COD_PRO ' +
          '  FROM ATS_OFCONS WHERE OFC_COFDET = ' + RefOf.Text;
  cdsP.CommandText := str1;
  cdsP.Open;
  buscaRefugo;

  RefProd.Items.Clear;
  while not cdsP.Eof do
  begin
    RefProd.Items.Add(cdsP.Fields[0].AsString);
    cdsP.Next;
  end;
end;

procedure TfRefugoRet.RefProdExit(Sender: TObject);
begin
  if (RefProd.Text <> '') then
    edProduto.Text := busca('SELECT PRO_DESCRI FROM F_PRODS WHERE PRO_CODPRO = ' +
      QuotedStr(RefProd.Text));
end;

procedure TfRefugoRet.buscaRefugo;
var sql: string;
begin
 { sql := 'SELECT ETI_CODETI, ETI_CODPRO, PRO_DESCRI, ETI_DATETI  ' +
    ' FROM F_ETIQUETAS, F_KARDEX, F_PRODS ' +
    'WHERE KAR_INDICE = ' + QuotedStrRCONS- || ETI_CODETI ' +
    '  AND ETI_CODPRO = PRO_CODPRO ' +
    '  AND ETI_IMPRES = 0';}
  if (cdsRef.Active) then
    cdsRef.Close;
  cdsRef.params[0].asString :=  edOf.Text;  
  //cdsRef.CommandText := sql;}
  cdsRef.Open;
end;

procedure TfRefugoRet.FormCreate(Sender: TObject);
begin
  QBusca.Close ;
  QBusca.SQL.Clear;
  QBusca.SQL.Add('SELECT * FROM L_LOCAL, F_LOCEST WHERE LOCAL = LCE_CODIGO');
  QBusca.Open;
  CBLocal.Clear;
  While not QBusca.Eof do
  Begin
    CBLocal.Items.Add(QBusca.Fields[0].AsString);
    QBusca.Next;
  End;
  QBusca.Close;
end;

procedure TfRefugoRet.BitBtn1Click(Sender: TObject);
var sqEtiq, s : String;
begin
  //inherited;
  fEtiqReimprime.btnProcurar.Enabled := False;
  sqEtiq := 'SELECT  ETI_CODETI, ETI_QTDENV, ETI_CODPRO, ETI_LOTE, NVL(ETI_COMP19,' +
    QuotedStr('0') + ') DOF_CODIOF, ' + ' NVL(0,0) DOF_CODIGO, ' +
    ' ETI_DATETI , substr(PRO_DESCRI, 0,37) PRO_DESCRI , ETI_IMPRES, ' +
    ' substr(PRO_DESCRI, 0, 80) PRO_DESCRI1, ETI_DATETI DOF_DATDOF, ETI_EATIVA DOF_CODTUR, ETI_LOCEST, PRO_QTDEMB ' +
    ' FROM F_ETIQUETAS, F_PRODS ' +
    ' WHERE ETI_CODPRO = PRO_CODPRO ' +
    ' AND ETI_COMP19 IS NULL ' +
    ' AND ETI_IMPRES = 0 ' +
    ' AND ETI_MODELO = 999 ';

  sqEtiq := sqEtiq + ' ORDER BY ETI_CODPRO, ETI_LOTE, ETI_CODETI ';
  if (fEtiqReimprime.cdsEtiqueta.Active) then
    fEtiqReimprime.cdsEtiqueta.Close;

  fEtiqReimprime.cdsEtiqueta.CommandText := sqEtiq;
  fEtiqReimprime.cdsEtiqueta.Open;
  fEtiqReimprime.ShowModal;
  fEtiqReimprime.btnProcurar.Enabled := True;
end;

end.
